<?php
#+------------------------------------------------------------------
#| 普通的。
#+------------------------------------------------------------------
#| Author:Janmas Cromwell <janmas-cromwell@outlook.com>
#+------------------------------------------------------------------
namespace form;

abstract class Html
{

    protected $html = '';

    /**
     * js逻辑代码
     * @var string
     */
    protected $js = '';

    /**
     * css代码
     * @var string
     */
    protected $css = '';

    /**
     * 加载js文件
     * @var string
     */
    protected $loadJs = '';

    /**
     * 加载css样式文件
     * @var string
     */
    protected $loadCss = '';

    /**
     * 标签跳转
     * @var string[]
     */
    protected $targetList = ['_self', '_blank', '_parent', '_top'];

    protected $enctypeList = [
        'application/x-www-form-urlencoded',
        'multipart/form-data',
        'text/plain',
        ''
    ];

    public function display()
    {
        if (!empty($this->loadCss)) {
            $this->html = str_pad($this->html, strlen($this->loadCss) + strlen($this->html), $this->loadCss, STR_PAD_LEFT);
        }

        if (!empty($this->css)) {
            $this->html = str_pad($this->html, strlen($this->css) + strlen($this->html) + strlen("<style>") * 2 + 1, "<style>" . $this->css . "</style>", STR_PAD_LEFT);

        }
        if (!empty($this->loadJs)) {
            $this->html .= $this->loadJs;
        }
        if (!empty($this->js)) {
            $this->html .= '<script>' . $this->js . '</script>';
        }
        return $this->html;
    }

    /**
     * 注入js逻辑代码
     * @param string $js
     */
    public function registerJs(string $js = ''): void
    {
        if (!empty($js)) {
            $this->js .= $js;
        }
    }

    /**
     * 注入css代码
     * @param string $css
     */
    public function registerCss(string $css): void
    {
        if (!empty($css)) {
            $this->css .= $css;
        }
    }

    /**
     * 注入js文件
     * @param $js
     */
    public function loadJs($js): void
    {
        if (is_array($js)) {
            array_map(function ($i) {
                $this->loadJs .= <<<HTML

<script type="application/javascript" src="$i"></script>

HTML;

            }, $js);
        } else if (!empty($js)) {
            $this->loadJs = <<<HTML
<script type="application/javascript" src="$js"></script>
HTML;
        }
    }

    /**
     * 注入css文件
     * @param $css
     */
    public function loadCss($css): void
    {
        if (is_array($css)) {
            array_map(function ($i) {
                $this->loadCss .= <<<HTML

    <link rel="stylesheet" href="$i">

HTML;

            }, $css);
        } else {
            $this->loadCss = <<<HTML
    <link rel="stylesheet" href="$css">
HTML;

        }
    }
}